<?php
require_once('Vega/Dao/Table.php');

class Mobitv_Admin_ScheduleDao extends Vega_Dao_Table
{

    public function getList($limit, $page, $condition = '')
    {
        //$package_id = intval($_GET['package_id']);
        $offset     = $limit * ($page - 1);
        $_limit     = ' LIMIT ' . $limit;
        $_offset    = ' OFFSET ' . $offset;

        if ($condition) {
            // condition = "c.id=$channel_id AND start_time LIKE ''";
            $sql = "SELECT cs.*, c.name as channel_name FROM channel_schedule cs LEFT JOIN channel c ON cs.channel_id = c.id WHERE $condition ORDER BY start_time DESC $_limit $_offset";
        } else {
            $sql = "SELECT cs.*, c.name as channel_name FROM channel_schedule cs LEFT JOIN channel c ON cs.channel_id = c.id ORDER BY start_time DESC $_limit $_offset";
        }
        $this->_db->query($sql);

        return $this->_db->fetchRowset();
    }

    public function countAll($condition) {
        if ($condition)
            $sql = "SELECT COUNT(cs.id) as numrow
                    FROM channel_schedule cs WHERE $condition";
        else
            $sql = "SELECT COUNT(id) as numrow
                    FROM channel_schedule";
        $this->_db->query($sql);
        $row = $this->_db->fetchRow();
        return $row['numrow'];
    }
    
    public function getDetail($scheduleId) {
        $sql = "SELECT * FROM channel_schedule WHERE id=$scheduleId";
        $this->_db->query($sql);
        return $this->_db->fetchRow();
    }

    public function save($data, $scheduleId) {
        $title = $this->_db->valueQuote($data['title']);
        $start_time = $this->_db->valueQuote($data['start_time']);
        $end_time = $this->_db->valueQuote($data['end_time']);
        $channel_id = intval($data['channel_id']);
        $isHot = intval($data['is_hot']);
        $category = $this->_db->valueQuote($data['category']);

        $sql = "UPDATE channel_schedule SET title=$title, channel_id=$channel_id, start_time=$start_time, end_time=$end_time, is_hot=$isHot, category=$category WHERE id=$scheduleId";
        return $this->_db->queryWrite($sql);
    }

    public function add($data) {
        $title = $this->_db->valueQuote($data['title']);
        $start_time = $this->_db->valueQuote($data['start_time']);
        $end_time = $this->_db->valueQuote($data['end_time']);
        $channel_id = intval($data['channel_id']);
        $isHot = intval($data['is_hot']);
        $category = $this->_db->valueQuote($data['category']);
        
        $sql = "INSERT INTO channel_schedule SET title=$title, channel_id=$channel_id, start_time=$start_time, end_time=$end_time, is_hot=$isHot, category=$category";
        return $this->_db->queryWrite($sql);
    }
    public function getId($data){
        $title = $this->_db->valueQuote($data['title']);
        $start_time = $this->_db->valueQuote($data['start_time']);
        $end_time = $this->_db->valueQuote($data['end_time']);
        $channel_id = intval($data['channel_id']);
        $isHot = intval($data['is_hot']);
        $category = $this->_db->valueQuote($data['category']);
        $sql = "SELECT id FROM channel_schedule WHERE title=$title AND channel_id=$channel_id AND start_time=$start_time AND end_time=$end_time AND is_hot=$isHot AND category=$category";
        $this->_db->query($sql);
        return $this->_db->fetchRow();
    }

    public function deleteFromList($list) {
        $list = "(" . implode(",", $list) . ")";
        $sql = "DELETE FROM channel_schedule WHERE id IN $list";
        return $this->_db->queryWrite($sql);
    }

    /*
     * Danh dau chuong trinh la hot
     */
    public function makeHot($list)
    {
        $list = "(" . implode(",", $list) . ")";
        $sql = "UPDATE channel_schedule SET is_hot=1 WHERE id IN $list";
        return $this->_db->queryWrite($sql);
    }
}